package com.ruoyi.dict.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.dict.domain.SysDictData2;
import com.ruoyi.dict.mapper.SysDictType2Mapper;
import com.ruoyi.dict.domain.SysDictType2;
import com.ruoyi.dict.service.ISysDictType2Service;

/**
 * 字典管理Service业务层处理
 * 
 * @author T348
 * @date 2023-11-29
 */
@Service
public class SysDictType2ServiceImpl implements ISysDictType2Service 
{
    @Autowired
    private SysDictType2Mapper sysDictType2Mapper;

    /**
     * 查询字典管理
     * 
     * @param dictId 字典管理主键
     * @return 字典管理
     */
    @Override
    public SysDictType2 selectSysDictType2ByDictId(Long dictId)
    {
        return sysDictType2Mapper.selectSysDictType2ByDictId(dictId);
    }

    /**
     * 查询字典管理列表
     * 
     * @param sysDictType2 字典管理
     * @return 字典管理
     */
    @Override
    public List<SysDictType2> selectSysDictType2List(SysDictType2 sysDictType2)
    {
        return sysDictType2Mapper.selectSysDictType2List(sysDictType2);
    }

    /**
     * 新增字典管理
     * 
     * @param sysDictType2 字典管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysDictType2(SysDictType2 sysDictType2)
    {
        sysDictType2.setCreateTime(DateUtils.getNowDate());
        int rows = sysDictType2Mapper.insertSysDictType2(sysDictType2);
        insertSysDictData(sysDictType2);
        return rows;
    }

    /**
     * 修改字典管理
     * 
     * @param sysDictType2 字典管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysDictType2(SysDictType2 sysDictType2)
    {
        sysDictType2.setUpdateTime(DateUtils.getNowDate());
        sysDictType2Mapper.deleteSysDictDataByDictType(sysDictType2.getDictType());
        insertSysDictData(sysDictType2);
        return sysDictType2Mapper.updateSysDictType2(sysDictType2);
    }

    /**
     * 批量删除字典管理
     * 
     * @param dictIds 需要删除的字典管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysDictType2ByDictIds(Long[] dictIds)
    {
        sysDictType2Mapper.deleteSysDictDataByDictTypes(dictIds);
        return sysDictType2Mapper.deleteSysDictType2ByDictIds(dictIds);
    }

    /**
     * 删除字典管理信息
     * 
     * @param dictId 字典管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysDictType2ByDictId(String dictId)
    {
        sysDictType2Mapper.deleteSysDictDataByDictType(dictId);
        return sysDictType2Mapper.deleteSysDictType2ByDictId(dictId);
    }

    /**
     * 新增字典数据信息
     * 
     * @param sysDictType2 字典管理对象
     */
    public void insertSysDictData(SysDictType2 sysDictType2)
    {
        List<SysDictData2> sysDictDataList = sysDictType2.getSysDictDataList();
        String dictType = sysDictType2.getDictType();
        if (StringUtils.isNotNull(sysDictDataList))
        {
            List<SysDictData2> list = new ArrayList<SysDictData2>();
            for (SysDictData2 sysDictData : sysDictDataList)
            {
                sysDictData.setDictType(dictType);
                list.add(sysDictData);
            }
            if (list.size() > 0)
            {
                sysDictType2Mapper.batchSysDictData(list);
            }
        }
    }
}
